package com.panda.web.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class BackgroundImageEvent extends PdfPageEventHelper {
    private Image backgroundImage;

    public BackgroundImageEvent(Image backgroundImage) {
        this.backgroundImage = backgroundImage;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            // 将背景图片添加到每一页的底部
            backgroundImage.scaleAbsolute(document.getPageSize().getWidth(), document.getPageSize().getHeight());
            backgroundImage.setAbsolutePosition(0, 0);
            writer.getDirectContentUnder().addImage(backgroundImage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}